<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 ,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>结算页面</title>
<link href="../static/css/amazeui.css" rel="stylesheet" type="text/css" />
<link href="../static/css/demo.css" rel="stylesheet" type="text/css" />
<link href="../static/css/cartstyle.css" rel="stylesheet" type="text/css" />
<link href="../static/css/jsstyle.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../static/js/address.js"></script>
<script type="text/javascript" src="../static/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="../static/js/myinit.js"></script>
<script type="text/javascript" src="../static/js/cook_shop_cart.js"></script>
</head>

<body>

<!--顶部导航条 -->
<div class="am-container header">
	<ul class="message-l">
		<div class="topMessage">
			<div class="menu-hd"> <a href="#" target="_top" class="h">亲，请登录</a> <a href="#" target="_top">免费注册</a> </div>
		</div>
	</ul>
	<ul class="message-r">
		<div class="topMessage home">
			<div class="menu-hd"><a href="#" target="_top" class="h">商城首页</a></div>
		</div>
		<div class="topMessage my-shangcheng">
			<div class="menu-hd MyShangcheng"><a href="#" target="_top"><i class="am-icon-user am-icon-fw"></i>个人中心</a></div>
		</div>
		<div class="topMessage mini-cart">
			<div class="menu-hd"><a id="mc-menu-hd" href="#" target="_top"><i class="am-icon-shopping-cart  am-icon-fw"></i><span>购物车</span><strong id="J_MiniCartNum" class="h">0</strong></a></div>
		</div>
		<div class="topMessage favorite">
			<div class="menu-hd"><a href="#" target="_top"><i class="am-icon-heart am-icon-fw"></i><span>收藏夹</span></a></div>
		</div>
	</ul>
</div>

<!--悬浮搜索框-->

<div class="nav white">
	<div class="logo"><img src="../static/img/logo.png" /></div>
	<div class="logoBig">
		<li><img src="../static/img/logobig.png" /></li>
	</div>
	<div class="search-bar pr"> <a name="index_none_header_sysc" href="#"></a>
		<form>
			<input id="searchInput" name="index_none_header_sysc" type="text" placeholder="搜索" autocomplete="off">
			<input id="ai-topsearch" class="submit am-btn" value="搜索" index="1" type="submit">
		</form>
	</div>
</div>
<div class="clear"></div>
<div class="concent">
<!--地址 -->
<div class="paycont">
<div class="address">
	<h3>确认收货地址 </h3>
	<div class="control">
		<div class="tc-btn createAddr theme-login am-btn am-btn-danger">使用新地址</div>
	</div>
	<div class="clear"></div>
	<ul id="addresses">
	</ul>
	<div class="clear"></div>
</div>
<!--物流 --> 
<!--
<div class="logistics">
  <h3>选择物流方式</h3>
  <ul class="op_express_delivery_hot">
    <li data-value="yuantong" class="OP_LOG_BTN  "><i class="c-gap-right" style="background-position:0px -468px"></i>圆通<span></span></li>
    <li data-value="shentong" class="OP_LOG_BTN  "><i class="c-gap-right" style="background-position:0px -1008px"></i>申通<span></span></li>
    <li data-value="yunda" class="OP_LOG_BTN  "><i class="c-gap-right" style="background-position:0px -576px"></i>韵达<span></span></li>
    <li data-value="zhongtong" class="OP_LOG_BTN op_express_delivery_hot_last "><i class="c-gap-right" style="background-position:0px -324px"></i>中通<span></span></li>
    <li data-value="shunfeng" class="OP_LOG_BTN  op_express_delivery_hot_bottom"><i class="c-gap-right" style="background-position:0px -180px"></i>顺丰<span></span></li>
  </ul>
</div>
<div class="clear"></div>
--> 
<!--支付方式
<div class="logistics">
  <h3>选择支付方式</h3>
  <ul class="pay-list">
  
    <li class="pay card"><img src="../static/img/wangyin.jpg" />银联<span></span></li>
    <li class="pay qq selected"><img src="../static/img/weizhifu.jpg" />微信<span></span></li>
	 <!--
    <li class="pay taobao"><img src="../static/img/zhifubao.jpg" />支付宝<span></span></li> 
  </ul>
</div>
<div class="clear"></div>
--> 
<!--订单 -->
<div class="concent">
<div id="payTable">
<h3>确认订单信息</h3>
<div class="cart-table-th">
	<div class="wp">
		<div class="th th-item">
			<div class="td-inner">商品信息</div>
		</div>
		<div class="th th-price">
			<div class="td-inner">单价</div>
		</div>
		<div class="th th-amount">
			<div class="td-inner">数量</div>
		</div>
		<div class="th th-sum">
			<div class="td-inner">金额</div>
		</div>
		 
	</div>
</div>
<div class="clear"></div>




<div id="shangpinglist" class="bundle  bundle-last">
	
</div>
<div class="clear"></div>
<div class="pay-total">
<!--留言-->
<div class="order-extra">
	<div class="order-user-info">
		<div id="holyshit257" class="memo">
			<label>买家留言：</label>
			<input type="text" id="remark" title="选填,对本次交易的说明（建议填写已经和卖家达成一致的说明）" placeholder="选填,建议填写和卖家达成一致的说明" class="memo-input J_MakePoint c2c-text-default memo-close">
			<div class="msg hidden J-msg">
				<p class="error">最多输入500个字符</p>
			</div>
		</div>
	</div>
</div>
<!--优惠券 -->
<div class="buy-agio">
<li class="td td-coupon"> <span class="coupon-title">优惠</span>
	<select data-am-selected id="youhuiquansousuo">
	 
	</select>
</li>
 
</div>
<div class="clear"></div>
</div>
<!--含运费小计 -->
<div class="buy-point-discharge ">
	<p class="price g_price "> 合计 <span>¥</span><em class="pay-sum" id="all_price"></em> </p>
</div>

<!--信息 -->
<div class="order-go clearfix">
	<div class="pay-confirm clearfix">
		<div class="box">
			<div tabindex="0" id="holyshit267" class="realPay"><em class="t">实付款：</em> <span class="price g_price "> <span>¥</span> <em class="style-large-bold-red " id="J_ActualFee"></em> </span> </div>
			<div id="holyshit268" class="pay-address">
				<p class="buy-footer-address"> <span class="buy-line-title buy-line-title-type">寄送至：</span> <span class="buy--address-detail" id="btm_addr"></span>  </p>
				<p class="buy-footer-address"> <span class="buy-line-title">收货人：</span> <span class="buy-address-detail"> <span class="buy-user" id="buyername"> </span> <span class="buy-phone" id="buyerphone"> </span> </span> </p>
			</div>
		</div>
		<div id="holyshit269" class="submitOrder">
			<div class="go-btn-wrap"> <a id="J_Go"   class="btn-go" tabindex="0" title="点击此按钮，提交订单">提交订单</a> </div>
		</div>
		<div class="clear"></div>
	</div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<div class="footer ">
	<div class="footer-hd">
		<p><img src="../static/img/duo.png" style="width:25px" />品类齐全<b>|</b><img src="../static/img/kuai.png" style="width:25px" />多仓直发<b>|</b><img src="../static/img/hao.png" style="width:25px" />正品行货<b>|</b><img src="../static/img/sheng.png" style="width:25px" />天天低价</p>
	</div>
	<div class="footer-bd "> </div>
</div>
</div>
<div class="theme-popover-mask"></div>
<!--
<div class="theme-popover"> 
	
	<!--标题 -->
	<!--
	<div class="am-cf am-padding">
		<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">新增地址</strong> / <small>Add address</small></div>
	</div>
	<hr/>
	<div class="am-u-md-12">
		<form class="am-form am-form-horizontal">
			<div class="am-form-group">
				<label for="user-name" class="am-form-label">收货人</label>
				<div class="am-form-content">
					<input type="text" id="user-name" placeholder="收货人">
				</div>
			</div>
			<div class="am-form-group">
				<label for="user-phone" class="am-form-label">手机号码</label>
				<div class="am-form-content">
					<input id="user-phone" placeholder="手机号必填" type="email">
				</div>
			</div>
			<div class="am-form-group">
				<label for="user-phone" class="am-form-label">所在地</label>
				<div class="am-form-content address">
					<select data-am-selected>
						<option value="a">浙江省</option>
						<option value="b">湖北省</option>
					</select>
					<select data-am-selected>
						<option value="a">温州市</option>
						<option value="b">武汉市</option>
					</select>
					<select data-am-selected>
						<option value="a">瑞安区</option>
						<option value="b">洪山区</option>
					</select>
				</div>
			</div>
			<div class="am-form-group">
				<label for="user-intro" class="am-form-label">详细地址</label>
				<div class="am-form-content">
					<textarea class="" rows="3" id="user-intro" placeholder="输入详细地址"></textarea>
					<small>100字以内写出你的详细地址...</small> </div>
			</div>
			<div class="am-form-group theme-poptit">
				<div class="am-u-sm-9 am-u-sm-push-3">
					<div class="am-btn am-btn-danger">保存</div>
					<div class="am-btn am-btn-danger close">取消</div>
				</div>
			</div>
		</form>
	</div>
</div>
-->
<div class="clear"></div>
<script type="text/javascript">
	ajaxSettingsasync(false);
	var openid='', all_price=0,addrid=-1;
	var youhuis=[];
	var addresses=[];
	getOpenid();
	initdizhilist();
	initshangpin();
	initmyyouhuiquan();
	
	
	
	
	function getOpenid(){
		var urll = '/weixin/getopenid/';
		var aa={};
		aa.code='';
		$.ajax({
			url : urll,
			data :  JSON.stringify(aa),
			async:isasync,
			contentType : "application/json;",
			dataType : "json",
			type : "POST",
			success :function(aa){
				openid = aa.openid;
			
			},
			error: function(){
			 	toastwithaction('尚未登录',function(){
					document.location.href="login_pc.html";
				});
					
			}
		});
	}
	
	
	


	function initdizhilist(){
		var url ='/webserver/api/v1/custadr/?format=json&wx_id='+openid;
		$.getJSON(url,function(rs){
			addresses=rs.objects;
			$('#addresses').empty();
			$.each(addresses,function(index,item){
				var defult='';
				var ishidden='';
				var moren='';
				if(address_lastid == item.id){
					defult="defaultAddr";
					ishidden="hidden";
					moren='<ins class="deftip">默认地址</ins> ';
					$('#btm_addr').html(item.cityname+item.detailAdrinfo);
					$('#buyername').html(item.custname);
					$('#buyerphone').html(item.phoneno);
					addrid=address_lastid;
				}; 
				$('#addresses').append('<li class="user-addresslist '+defult+'" id="'+item.id+'"><div class="address-left"><div class="user DefaultAddr"> <span class="buy-address-detail"> <span class="buy-user">'+item.custname+' </span> <span class="buy-phone">'+item.phoneno+'</span> </span> </div><div class="default-address DefaultAddr"> <span class="buy-line-title buy-line-title-type">收货地址：</span> <span class="buy--address-detail"> '+item.cityname+item.detailAdrinfo+' </span> </span> </div>'+moren+'</div><div class="address-right"> <a href="person/address.html"> <span class="am-icon-angle-right am-icon-lg"></span></a> </div><div class="clear"></div><div class="new-addr-btn"> <a   class="'+ishidden+'">设为默认</a> <span class="new-addr-bar hidden">|</span>  </div></li>');
				$('#addresses #'+item.id).find('a').click(function(e) {
					var htm = $(this).html();
					if(htm == '设为默认'){
						savemylast(item.id);
						alert('aa');
						initdizhilist();
						
						
					}
				});
				$('#addresses #'+item.id).click(function(){
					$('#btm_addr').html(item.cityname+item.detailAdrinfo);
					$('#buyername').html(item.custname);
					$('#buyerphone').html(item.phoneno);
					addrid=item.id;
					
				});
				
			});
			 
		});
	}
	
	function initshangpin(){
		var ids=getpayshangpingids();
		console.log(ids);
		$.each(ids,function(index,item){
			var idd=item.ID;
			var sp = cartHelper.Finditem(idd);
			addshangping(sp.img,sp.name,sp.xiangqing,sp.count,sp.price);
		
		});
		 
	}
	
	
	
	function addshangping(img,name,detail,count,price){
		var aa='<div class="bundle-main"><ul class="item-content clearfix"><div class="pay-phone"><li class="td td-item"><div class="item-pic"> <a class="J_MakePoint"> <img src="'+img+'" style="width:80px;height:auto" class="itempic J_ItemImg"></a> </div><div class="item-info"><div class="item-basic-info"> <a class="item-title J_MakePoint" data-point="tbcart.8.11">'+name+'</a> </div></div></li><li class="td td-info"><div class="item-props"> '+detail+' </div></li><li class="td td-price"><div class="item-price price-promo-promo"><div class="price-content"> <em class="J_Price price-now">'+price+'</em> </div></div></li></div><li class="td td-amount"><div class="amount-wrapper "><div class="item-amount "> <span class="phone-title">购买数量</span><div class="sl" style="margin-top: 5px;"> '+count+' </div></div></div></li><li class="td td-sum"><div class="td-inner"> <em tabindex="0" class="J_ItemSum number">'+(price*count*0.01).toFixed(2)+'</em> </div></li> </ul><div class="clear"></div></div>';
	
		$('#shangpinglist').append(aa);
		all_price+=parseInt(price*count*0.01);
	
	}
	
	function initmyyouhuiquan(){
		
		var url = 'http://www.tiantianhw.cn/webserver/api/v1/deductionitem/?format=json&openid='+openid+'&status=已领取';		 
		$.getJSON(url,function(rs){
			youhuis=rs.objects;
			$.each(youhuis,function(index,item){
				if(!(item.deduction.condi_value>all_price)){
					$('#youhuiquansousuo').append('<option value="'+item.id+'"><div class="c-price">'+item.deduction.name+'</div></option>');
				}
			});
		},'json');
		refreshallprice();
		 
	}
	
	function refreshallprice(){
		var va=$('#youhuiquansousuo').val();
		var all=all_price;
		$.each(youhuis,function(index,item){
			if(va==item.id){
				all-=item.deduction.deduct_value;
			}
		});
		$('#all_price').html(all_price);
		$('#J_ActualFee').html(all.toFixed(2));
		
		
		
	}
	$('#youhuiquansousuo').change(function(){
		refreshallprice();
	});
	
	$('#J_Go').click(function(){
		cmfdingdan();
	});
	
	
	function cmfdingdan(){
		if(addrid==-1){
			alert('请先选择地址');
			return;
		}
		
		//优惠券
		var yhid=$('#youhuiquansousuo').val();
		 
		
		
		
		
		var url = "/shop/api/addorder/";
		var aa={};
		aa.openid=openid;
		aa.custadr=addrid;
		aa.beizhu="";
		aa.detail=[];
		if(yhid!=''&&yhid!=null&&yhid!='null'){
			aa.deductionitem=yhid;
		}else{
			aa.deductionitem=0;
		}
		
		var ids=getpayshangpingids();
		$.each(ids,function(index,item){
			var idd=item.ID;
			var sp = cartHelper.Finditem(idd);
			var bb={};
			bb.id=sp.goodid;
			bb.count=sp.count;
			bb.xiangqing=sp.xiangqing;
			bb.price=sp.price*sp.count*0.01;
			aa.detail.push(bb);
		
		});
		var order_remark=$('#remark').val();
		aa.customer_remark=order_remark;
		aa.order_remark=order_remark;
		
		console.log(aa);
		dopost(url,aa,function(rs){
			//{"orderid": 143}
			if(rs.orderid>0){
				var ids=getpayshangpingids();
				$.each(ids,function(index,item){
					var idd=item.ID;
					cartHelper.Del(idd);
				
				});
				document.location.href="success_pc.html?ORDERID="+rs.orderid;
				
			}
			
			 
		});
		
	}
	
	 

</script>
</body>
</html>